var t = require("../../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../../@babel/runtime/regenerator")), e = require("../../../../@babel/runtime/helpers/asyncToGenerator"), a = getApp();

Page({
    data: {
        showProjects: !1,
        show1: !1,
        id: 0,
        project_id: 0,
        project: {},
        store: [],
        chooseTechnician: {
            name: ""
        },
        result: "",
        activeNames: [ "1" ],
        times: [],
        calendarEvents: [],
        isRest: !1,
        AStore: {},
        applyModel: {
            order_id: "",
            store_id: "",
            worker_id: "",
            customer_name: "",
            customer_tel: "",
            begin_time: ""
        },
        work_date: "",
        start_time: "",
        point: {},
        appointmentLang: {}
    },
    onLoad: function(t) {
        this.data.id = t.id, this.data.project_id = t.project_id, this.getData(), this.getLocation(), 
        this.data.today = new Date().getFullYear() + "-" + (new Date().getMonth() + 1 >= 10 ? new Date().getMonth() + 1 : "0" + (new Date().getMonth() + 1)) + "-" + (new Date().getDate() >= 10 ? new Date().getDate() : "0" + new Date().getDate()), 
        this.data.work_date = this.data.today;
        var e = wx.getStorageSync("yz_basic_info");
        this.setData({
            appointmentLang: e.lang.appointment
        }), console.log(e.lang), this.setAppointment(t);
    },
    onReady: function() {},
    setAppointment: function(t) {
        t.has_one_store && this.setData({
            AStore: JSON.parse(t.has_one_store)
        }), t.store_id && this.setData({
            "applyModel.store_id": t.store_id
        }), t.has_one_worker && this.setData({
            chooseTechnician: JSON.parse(t.has_one_worker)
        });
    },
    closeProjects: function(t) {
        this.setData({
            showProjects: !this.data.showProjects
        });
    },
    onShow: function() {},
    onHide: function() {},
    closeStore: function() {
        this.setData({
            show1: !1
        });
    },
    changeCollapse: function(t) {
        this.setData({
            activeNames: t.detail
        });
    },
    onUnload: function() {},
    onPullDownRefresh: function() {},
    inputCustomerName: function(t) {
        console.log(t.detail.value), this.data.applyModel.customer_name = t.detail.value;
    },
    inputCustomerTel: function(t) {
        this.data.applyModel.customer_tel = t.detail.value;
    },
    inputRemark: function(t) {
        this.data.applyModel.remark = t.detail.value;
    },
    onReachBottom: function() {},
    chooseStore: function(t) {
        var e = t.currentTarget.dataset.item;
        this.setData({
            AStore: e,
            "applyModel.store_id": e.id,
            show1: !1,
            start_time: "",
            "chooseTechnician.name": ""
        });
    },
    toTime: function() {
        if (!this.data.applyModel.store_id) return this.tips("请先选择门店");
        this.setData({
            activeNames: [ "1" ]
        }), this.getDate(), this.getTime();
    },
    onchangeRadio: function(t) {
        console.log(t), this.setData({
            result: t.detail
        });
    },
    onShareAppMessage: function() {},
    getDate: function() {
        var t = this, e = a.getNetAddresss("plugin.appointment.frontend.work-time.get-work-dates");
        a._getNetWork({
            url: e,
            data: {
                store_id: this.data.applyModel.store_id,
                project_id: this.data.project_id
            },
            success: function(e) {
                var a = e.data;
                if (1 !== a.result) return t.tips(a.msg);
                var i = a.data.map(function(e) {
                    return e.work_date_format == t.data.today && (1 == e.work_sattus ? t.data.isRest = !0 : t.data.isRest = !1), 
                    1 == e.work_sattus ? e.work_date_format + "-休" : 2 == e.work_sattus ? e.work_date_format + "-约满" : e.work_date_format;
                });
                console.log(a.data), t.setData({
                    showProjects: !0,
                    calendarEvents: i
                });
            }
        });
    },
    getTime: function() {
        var t = this, e = a.getNetAddresss("plugin.appointment.frontend.work-time.get-start-times");
        a._getNetWork({
            url: e,
            data: {
                store_id: this.data.applyModel.store_id,
                project_id: this.data.project_id,
                work_date: this.data.work_date
            },
            success: function(e) {
                var a = e.data;
                if (1 !== a.result) return t.tips(a.msg);
                t.setData({
                    times: a.data
                });
            }
        });
    },
    getData: function() {
        var t = this, e = a.getNetAddresss("plugin.appointment.frontend.appointment-order.detail");
        a._getNetWork({
            url: e,
            data: {
                project_id: this.data.id
            },
            success: function(e) {
                var a = e.data;
                if (1 !== a.result) return t.tips(a.msg);
                a.data.default_customer && t.setData({
                    "applyModel.customer_tel": a.data.default_customer.tel,
                    "applyModel.customer_name": a.data.default_customer.name
                }), t.setData({
                    project: a.data.project
                });
            }
        });
    },
    handSelect: function(t) {
        console.log(t), this.setData({
            work_date: t.detail.work_date,
            start_time: "",
            isRest: !t.detail.disabled
        }), this.getTime();
    },
    selectedTime: function(t) {
        this.setData({
            activeNames: !1,
            start_time: t.currentTarget.dataset.time
        }), this.getTechnician();
    },
    clickTechnician: function(t) {
        this.setData({
            chooseTechnician: t.currentTarget.dataset.item
        }), console.log(t.currentTarget.dataset.item);
    },
    makeAppointment: function() {
        var t = this;
        if (!this.data.start_time) return this.tips("请选择时间");
        if (!this.data.chooseTechnician.name) return this.tips("请选择" + this.data.appointmentLang.worker);
        if (!this.data.applyModel.customer_name) return this.tips("请填写姓名");
        if (!this.data.applyModel.customer_tel) return this.tips("请填写电话");
        this.data.applyModel.order_id = this.data.id, this.data.applyModel.worker_id = this.data.chooseTechnician.id, 
        this.data.applyModel.begin_time = this.data.work_date + " " + this.data.start_time;
        var e = a.getNetAddresss("plugin.appointment.frontend.appointment-service.create-service");
        a._postNetWork({
            url: e,
            data: this.data.applyModel,
            success: function(e) {
                var i = e.data;
                if (1 !== i.result) return t.tips(i.msg);
                a.confirm("预约成功", function() {
                    wx.navigateBack();
                }, !1);
            }
        });
    },
    getTechnician: function() {
        var t = this, e = a.getNetAddresss("plugin.appointment.frontend.worker.get-appointment-workers");
        a._getNetWork({
            url: e,
            data: {
                store_id: this.data.applyModel.store_id,
                project_id: this.data.project_id,
                work_date: this.data.work_date,
                start_time: this.data.start_time
            },
            success: function(e) {
                var a = e.data;
                if (1 !== a.result) return t.tips(a.msg);
                t.setData({
                    technician: a.data
                });
            }
        });
    },
    getStore: function() {
        var t = this;
        if (this.data.point.lng) {
            var e = a.getNetAddresss("plugin.appointment.frontend.store.get-list-by-project-id");
            a._getNetWork({
                url: e,
                data: {
                    lng: this.data.point.lng,
                    lat: this.data.point.lat,
                    project_id: this.data.project_id
                },
                success: function(e) {
                    var a = e.data;
                    if (1 !== a.result) return t.tips(a.msg);
                    t.setData({
                        store: a.data,
                        show1: !0
                    });
                }
            });
        } else this.getLocation();
    },
    getLocation: function() {
        var a = this;
        return e(t.default.mark(function e() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    wx.getLocation({
                        type: "wgs84",
                        success: function(t) {
                            a.setData({
                                "point.lat": t.latitude,
                                "point.lng": t.longitude
                            });
                        }
                    });

                  case 1:
                  case "end":
                    return t.stop();
                }
            }, e);
        }))();
    },
    checkAlbum: function(a) {
        var i = this;
        return new Promise(function() {
            var n = e(t.default.mark(function e(n, s) {
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.next = 2, i.getAuthSetting(a);

                      case 2:
                        t.sent ? n(!0) : wx.showModal({
                            title: "请开启授权",
                            content: "是否跳转设置页开启授权",
                            success: function(t) {
                                t.confirm ? wx.openSetting({
                                    success: function(t) {
                                        t.authSetting[a] ? n(!0) : n(!1);
                                    }
                                }) : t.cancel && n(!1);
                            },
                            fail: function() {
                                n(!1);
                            }
                        });

                      case 4:
                      case "end":
                        return t.stop();
                    }
                }, e);
            }));
            return function(t, e) {
                return n.apply(this, arguments);
            };
        }());
    },
    getAuthSetting: function(t) {
        return new Promise(function(e, a) {
            wx.getSetting({
                success: function(a) {
                    a.authSetting[t] ? e(!0) : wx.authorize({
                        scope: t,
                        success: function() {
                            e(!0);
                        },
                        fail: function() {
                            e(!1);
                        }
                    });
                },
                fail: function() {
                    e(!1);
                }
            });
        });
    },
    tips: function(t) {
        return wx.showToast({
            title: t,
            icon: "none",
            duration: 2e3
        }), !1;
    }
});